﻿function setupReadOnlyAcceptanceCriteriaRow(acceptanceCriteriaId, storyId) {
    $(".AcceptanceCriteria_" + acceptanceCriteriaId).click(function () {
        var url = addURLRamdon('/AcceptanceCriteria/Edit');
        $.post(url,
            {
                id: acceptanceCriteriaId
            },
            function (data) {
                $("#AcceptanceCriteriaTableBody_" + storyId + " .taskRow").removeClass("taskAltRows");
                $('#AcceptanceCriteriaRow_' + acceptanceCriteriaId).replaceWith(data);
                $("#AcceptanceCriteriaTableBody_" + storyId + " .taskRow:odd").addClass("taskAltRows");
            }
        );
    });

    $(document).ready(function () {
        $("#AcceptanceCriteriaTableBody_" + storyId + " .AcceptanceCriteriaRow:odd").addClass("taskAltRows");
    });

    return;
}

function cancelAcceptanceCriteriaRowEdit(acceptanceCriteriaId, storyId) {
    if (acceptanceCriteriaId > 0) {
        var url = addURLRamdon('/AcceptanceCriteria/ReadOnly');
        $.post(url,
            {
                id: acceptanceCriteriaId
            },
            function (data) {
                $('#AcceptanceCriteriaRow_' + acceptanceCriteriaId).replaceWith(data);
            }
        );
    }
    else {
        var target = $('#AcceptanceCriteriaTable_' + storyId + ' tbody tr:first');
        if (target.length > 0)
            $('#AcceptanceCriteriaTable_' + storyId + ' tbody tr:first').remove();
    }
    return;
}

function addAcceptanceCriteriaRow(storyId) {
    $("#AcceptanceCriteriaTableBody_" + storyId + " .taskRow").removeClass("taskAltRows");
    var url = addURLRamdon('/AcceptanceCriteria/New');
    $.post(url,
        {
            storyId: storyId
        },
        function (data) {
            var target = $('#AcceptanceCriteriaTable_' + storyId + ' tbody tr:first');
            if (target.length > 0)
                $('#AcceptanceCriteriaTable_' + storyId + ' tbody tr:first').before(data);
            else
                $('#AcceptanceCriteriaTable_' + storyId + ' tbody').prepend(data);
        }
    );

        $("#AcceptanceCriteriaTableBody_" + storyId + " .AcceptanceCriteriakRow:odd").addClass("taskAltRows");
    return;
}

//function hideBottomContainerBorderIfNeeded(storyId) {
//    // if there are no tasks, then hide the dotted container bottom border
//    var targetTag = $('#taskTable_' + storyId + ' tbody :first-child');
//    if (targetTag.length == 0)
//        $('#taskContainerId_' + storyId).css('border-bottom', '0px');
//    else
//        $('#taskContainerId_' + storyId).css('border-bottom', '1px dotted #444');
//    return;
//}

function saveAcceptanceCriteriaRowEdit(acceptanceCriteriaId, storyId) {
    
    var description = $('#AcceptanceCriteriaDescription_' + acceptanceCriteriaId).val();
    var acceptanceCriteria1 = $('#AcceptanceCriteriaAcceptanceCriteria1_' + acceptanceCriteriaId).val();


    if (acceptanceCriteriaId > 0) {
        var url = addURLRamdon('/AcceptanceCriteria/Save');
        $.post(url,
            {
                storyId: storyId, acceptanceCriteriaId: acceptanceCriteriaId,
                description: description, acceptanceCriteria1: acceptanceCriteria1
            },
            function (data) {
                $('#AcceptanceCriteriaRow_' + acceptanceCriteriaId).replaceWith(data);                
            }
        );              
        
    }
        else {
        var url = addURLRamdon('/AcceptanceCriteria/Save');
        $.post(url,
            {
                storyId: storyId, acceptanceCriteriaId: acceptanceCriteriaId,
                description: description, acceptanceCriteria1: acceptanceCriteria1
            },
            function (data) {
                $("#AcceptanceCriteriaTableBody_" + storyId + " .taskRow").removeClass("taskAltRows");
                $('#AcceptanceCriteriaTableBody_' + storyId).replaceWith(data);
                $("#AcceptanceCriteriaTableBody_" + storyId + " .taskRow:odd").addClass("taskAltRows");                
            }
        );
    }
    
    // TODO: Implement save failure GUI
}





// TODO: ************  Turn total into a number

//function setTaskTotalHours(storyId) {

//    $.post('/Task/TotalTaskHours',
//        {
//            storyId: storyId
//        },
//        function (data) {

//            $('#storyTotalHours_' + storyId).text(getTaskHourString(Number(data)));
//            $('#totalTaskHoursTaskHeader_' + storyId).text('(' + data + ')');
//        }
//    );
//}

//function getTaskHourString(taskHours){
//    var taskHourString = "";
//    if (taskHours > 1 || taskHours == 0 || taskHours < 0)
//        taskHourString = taskHours + " hrs";
//    else
//        taskHourString = "1 hr";
//    return taskHourString;
//}









function deleteAcceptanceCriteria(storyId, acceptanceCriteriaId) {


    jConfirm('This action removes the element. Are you sure you want to continue?', 'Confirmation Dialog', function (r) {
        if (!r) return;
        var url = addURLRamdon('/AcceptanceCriteria/Delete');
        $.post(url,
        {
            id: acceptanceCriteriaId,
            acceptanceCriteriaId: acceptanceCriteriaId,
            storyId: storyId
        },
        function (data) {

            $("#AcceptanceCriteriaTableBody_" + storyId + " .taskRow").removeClass("taskAltRows");
            $('#AcceptanceCriteriaTableBody_' + storyId).replaceWith(data);
            $("#AcceptanceCriteriaTableBody_" + storyId + " .taskRow:odd").addClass("taskAltRows");
        });
    });

   

    // TODO: Implement delete failure GUI
}


